/**
 * Created by JLB1 on 2017/2/7.
 */
import React, {Component, PropTypes} from 'react'
require('./Sliders.less')
import AJAX from "../../../lib/ajax";
import Axios from "axios";
require('es6-promise').polyfill();
const propTypes = {
    link: PropTypes.string,
    src: PropTypes.string
}

const defaultProps = {
    link: "javascript:;"
}

class Sliders extends Component {
    constructor(props) {
        super(props)
        this.handleStatAd = this.handleStatAd.bind(this);
    }

    handleStatAd(id, title, link) {
        // 新增广告统计
        AJAX.request('advertSave', {advertId: id, type: 2}, (data) => {
            const { code, body } = data;
            if (code === 0) {
                console.log('记录成功', body);
            }
        });
        AJAX.request("stat_advert", {advert_id: id, action:1}, (data) => {
            window.location.href = link
        });
    }

    render() {
        var picStyles = {
            backgroundImage: "url(" + this.props.src + ")"
        }
        var aStyles = {
            width: document.documentElement.clientWidth + "px"
        }
        const link = this.props.link ? (this.props.link.indexOf('http://') >= 0 || this.props.link.indexOf('https://') >= 0 ) ? this.props.link : `http://` + this.props.link : "javascript:;"
        return (
            <a href="javascript:;" 
                onClick={this.handleStatAd.bind(this, this.props.id, this.props.title, link)} 
                className="slide-a"
                style={aStyles}>
                {/*<div  className="slide-li" ></div>*/}
                <img className="slide-li" src={this.props.src} alt=""/>
            </a>
        )
    }
}

Sliders.propTypes = propTypes
Sliders.defaultProps = defaultProps

export default Sliders;